package com.in.config;


import com.in.utils.Result;
import com.in.utils.StatusCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.io.IOException;
import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;
import java.sql.SQLSyntaxErrorException;

/**
 * @Author liulianjia
 * @Description 全局异常处理
 * @Date 2024/4/9 10:32
 **/
@Slf4j
@RestControllerAdvice(annotations = {RestController.class, Controller.class})
public class GlobalExceptionHandler {

    /**
     * @Author liulianjia
     * @Description IO异常
     * @Date 2024/4/9 10:31
     **/
    @ExceptionHandler(IOException.class)
    public Result ioException(IOException e) {
        log.error("IO 异常，详细信息:{}", e.getLocalizedMessage(), e);
        String msg = "系统检测到超时异常：" + e.getMessage();
        return Result.err(StatusCode.SERVICE_UNAVAILABLE, msg);
    }

    /**
     * @Author liulianjia
     * @Description 空指针异常
     * @Date 2024/4/9 10:32
     **/
    @ExceptionHandler(NullPointerException.class)
    public Result nullPointer(NullPointerException e) {
        log.error("NULL Point 异常，详细信息:{}", e.getLocalizedMessage(), e);
        String msg = "系统检测到空指针异常：" + e.getMessage();
        return Result.err(StatusCode.SERVICE_UNAVAILABLE, msg);
    }

    /**
     * @Author liulianjia
     * @Description 为空异常 @NotNull
     * @Date 2024/4/9 10:34
     **/
    @ExceptionHandler(IllegalStateException.class)
    public Result illegalStateException(IllegalStateException e) {
        log.error("Illegal state exception 异常，详细信息:{}", e.getLocalizedMessage(), e);
        String msg = "系统检测到空值异常：" + e.getMessage();
        return Result.err(StatusCode.DATA_ERROR, msg);
    }

    /**
     * @Author liulianjia
     * @Description SQL异常
     * @Date 2024/4/11 10:54
     **/
    @ExceptionHandler(SQLSyntaxErrorException.class)
    public Result sqlSyntaxErrorException(SQLSyntaxErrorException e) {
        log.error("Sql syntax error exception 异常，详细信息:{}", e.getLocalizedMessage(), e);
        String msg = "系统检测到数据查询异常：" + e.getMessage();
        return Result.err(StatusCode.EXIT, msg);
    }

    /**
     * @Author liulianjia
     * @Description 唯一异常
     * @Date 2024/4/11 10:54
     **/
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public Result sqlIntegrityConstraintViolationException(SQLIntegrityConstraintViolationException e) {
        log.error("Sql Integrity Constraint Violation exception 异常，详细信息:{}", e.getLocalizedMessage(), e);
        String msg = "";
        if (e.getMessage().contains("Duplicate entry")) {
            msg = "系统检测到数据唯一验证异常：" + e.getMessage() + "，请修改！";
        }
        return Result.err(StatusCode.ERROR_CODE, msg);
    }

    /**
     * @Author liulianjia
     * @Description 数据库操作异常
     * @Date 2024/4/11 10:54
     **/
    @ExceptionHandler(SQLException.class)
    public Result sqlException(SQLException e) {
        log.error("Sql exception 异常，详细信息:{}", e.getLocalizedMessage(), e);
        String msg = "系统检测到数据库操作异常：" + e.getMessage() + "，请修改！";

        return Result.err(StatusCode.EXIT, msg);
    }

    /**
     * @Author liulianjia
     * @Description validation 异常
     * @Date 2024/11/27 14:15
     **/
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Result sqlException(MethodArgumentNotValidException e) {
        log.error("validation exception 异常，详细信息:{}", e.getLocalizedMessage(), e);
        String msg = "系统检测到参数异常：" + e.getMessage() + "，请修改！";

        return Result.err(StatusCode.EXIT, msg);
    }
}
